package physics;
import java.awt.Color;
import java.awt.Graphics2D;

/**
 * @author Sam Rang and Kennan Raju
 */
public class Bar extends Connector {
    private static final int LARGE_KVAL = 4;
    private double myMass1;
    private double myMass2;
    private double myLastLength;

    /**
     * The constructor for this object, referencing the abstract
     * connector class.
     * @param start The mass at one end of the Bar
     * @param end The mass at the other end of the Bar
     * @param length The length of the Bar
     */
    public Bar (Mass start, Mass end, double length) {
        super(start, end, length, 0);
        myMass1 = start.getMass();
        myMass2 = end.getMass();
        myLastLength = getLength();
    }

    /**
     * Acts like a spring with a large k-value and with
     * dampening which makes it so that if the bar isn't
     * at its proper length it will expand but not oscillate.
     * @param dx Component of distance between the two end masses
     * on the x-axis
     * @param dy Component of distance between the two end masses
     * on the y-axis
     */
    @Override
    public Force setForce(double dx, double dy) {
        double c  = Math.sqrt((myMass1 + myMass2) * LARGE_KVAL);
        double len = Force.distanceBetween(dx, dy);
        Force f = new Force(Force.angleBetween(dx, dy),
                LARGE_KVAL * (getLength() - len)
                - 2 * c * (len - myLastLength));
        myLastLength = len;
        return f;
    }

    @Override
    public void checkStyle (double len, double restLength, Graphics2D pen) {
        pen.setColor(Color.RED);
    }
}
